<?php

use Illuminate\Database\Seeder;
use App\Models\Permission;
use App\Models\Role;
use App\Models\Menu;
use Illuminate\Support\Facades\DB;

class RolesAndPermissionsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 重置角色和权限缓存
        app()['cache']->forget('spatie.permission.cache');

        // 初始化权限
        $permissions = json_decode(file_get_contents(database_path('data/permission.json')), true);

        foreach ($permissions as $permission) {
            if (isset($permission['children'])) {
                $children = $permission['children'];
                unset($permission['children']);
                $parent = Permission::create($permission);
                $parent->children()->createMany($children);
            } else {
                Permission::create($permission);
            }
        }

        // 初始化角色
        $roles = json_decode(file_get_contents(database_path('data/role.json')), true);

        foreach ($roles as $role) {
            Role::create($role);
        }

        $roles = Role::all();
        foreach ($roles as $role) {
            $menus = [];
            $permissions = [];

            switch ($role['name']) {
                case 'admin':
                    $menus = Menu::whereNotExists(function ($query) {
                        $query->select(DB::raw(1))->from('menus as m2')->whereRaw('m2.pid = menus.id');
                    })->pluck('id');

                    $permissions = Permission::whereNotExists(function ($query) {
                        $query->select(DB::raw(1))->from('permissions as p2')->whereRaw('p2.pid = permissions.id');
                    })->get();
                    break;

                case 'model':
                case 'photographer':
                    $menus = Menu::whereIn('name', ['home', 'photos', 'users'])->pluck('id');

                    $permissions = Permission::whereIn('name', [
                        'home.index',
                        'password.update',
                        'upload.store',
                        'users.index', 'users.show', 'users.info', 'users.avatar', 'users.profile', 'users.status', 'users.store', 'users.update', 'users.destroy',
                        'plans.index', 'plans.show', 'plans.store', 'photos.store', 'photos.retouch', 'photos.update', 'photos.comments', 'photos.toggle', 'photos.destroy', 'plans.update', 'plans.destroy'
                    ])->get();
                    break;

                case 'customer':
                    $menus = Menu::whereIn('name', ['home', 'photos'])->pluck('id');

                    $permissions = Permission::whereIn('name', [
                        'home.index',
                        'password.update',
                        'upload.store',
                        'users.info', 'users.avatar', 'users.profile',
                        'plans.index', 'plans.show', 'photos.comments',  'photos.toggle'
                    ])->get();
                    break;
            }

            $role->menus()->sync($menus);
            $role->givePermissionTo($permissions);
        }
    }
}
